package com.chefu.order.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chefu.common.pojo.CfException;
import com.chefu.common.pojo.ExceptionEnum;
import com.chefu.common.pojo.PageResult;
import com.chefu.common.utils.IdWorker;
import com.chefu.order.dto.OrderDTO;
import com.chefu.order.mapper.OrderMapper;
import com.chefu.order.mapper.RecordMapper;
import com.chefu.order.pojo.Order;
import com.chefu.order.pojo.OrderStatusEnum;
import com.chefu.order.pojo.PayStatusEnum;
import com.chefu.order.pojo.Record;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private RecordMapper recordMapper;


    /**
     * 创建订单
     *
     * @param order
     */

    public Long buildOrder(OrderDTO orderDTO) {
        //创建订单
        Order order = new Order();
        //订单id
        order.setOrderServiceId(idWorker.nextId());
        //订单服务商编号
        order.setMerchantId(orderDTO.getMerchantId());
        //订单商家名称
        order.setShortName(orderDTO.getShortName());
        //订单商家手机号
        order.setMerchantPhone(orderDTO.getMerchantPhone());
        //车主编号
        order.setUserId(orderDTO.getUserId());
        //联系人姓名、手机号
        order.setOrderPerson(orderDTO.getOrderPerson());
        order.setOrderPhone(orderDTO.getOrderPhone());
        //预约时间
        order.setOrderTime(orderDTO.getOrderTime());
        //预约描述
        order.setDescribes(orderDTO.getDescribes());
        //车辆编号
        order.setCarId(orderDTO.getCarId());
        //服务项目ids
        order.setServeIds(orderDTO.getServeIds());
        //金额
        order.setPrice(orderDTO.getPrice());
        //订单状态
        order.setOrderStatus(OrderStatusEnum.INIT.value());
        //支付状态
        order.setPayStatus(PayStatusEnum.INIT.value());
        //创建时间
        order.setCreateTime(new Date());
        orderMapper.insert(order);

        //创建记录表
        Record record = getRecord(order.getOrderServiceId(), String.valueOf(order.getOrderStatus()),order.getMerchantId(),order.getShortName());
        recordMapper.insert(record);
        return order.getOrderServiceId();
    }

    /**
     * 分页过滤条件查询
     *
     * @return
     */
    public PageResult<Order> findByPage(Integer pageNo, Integer pageSize, Long orderServiceId, String orderPerson,
                                        String shortName, String startTime, String endTime, String orderStatus, String payStatus) {
        //1.封装条件
        //1.1 封装分页参数
        IPage<Order> iPage = new Page(pageNo, pageSize);

        //1.2 封装查询条件
        QueryWrapper<Order> queryWrapper = Wrappers.query();

        //拼接条件
        //处理客户名称
        if (StringUtils.isNotEmpty(orderPerson)) {
            queryWrapper.like("order_person", orderPerson);
        }
        //处理商家名称
        if (StringUtils.isNotEmpty(shortName)) {
            queryWrapper.like("short_name", shortName);
        }
        //处理订单编号
        if (orderServiceId != null) {
            queryWrapper.like("order_service_id", orderServiceId);
        }
        //处理订单时间段
        if (startTime != null & endTime != null) {
            queryWrapper.between("create_time", startTime, endTime);
        }
        //处理订单状态
        if (orderStatus != null) {
            queryWrapper.like("order_status", orderStatus);
        }
        //处理支付状态
        if (payStatus != null) {
            queryWrapper.like("pay_status", payStatus);
        }

        //2.查询数据
        iPage = orderMapper.selectPage(iPage, queryWrapper);

        //3.处理并返回结果
        List<Order> orderList = iPage.getRecords();
        PageResult<Order> pageResult = new PageResult<>(iPage.getTotal(), iPage.getPages(), orderList);
        return pageResult;
    }


    //记录改变表状态
    public void changeStatus(Long orderServiceId, String orderStatus, Long merchantId, String employeeName) {
        Order order = orderMapper.selectById(orderServiceId);
        order.setOrderStatus(Integer.valueOf(orderStatus));
        orderMapper.updateById(order);
        Record record = getRecord(orderServiceId, orderStatus, merchantId, employeeName);
        recordMapper.updateById(record);
    }

    //设置记录表
    public Record getRecord(Long orderServiceId, String orderStatus, Long merchantId, String employeeName) {
        Record record = new Record();
        record.setOrderServiceId(orderServiceId);
        record.setOrderStatus(Integer.valueOf(orderStatus));
        record.setMerchantId(merchantId);
        record.setEmployeeName(employeeName);
        record.setUpdateTime(new Date());
        return record;
    }
}
